package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou3g.musicenjoy.R;

import java.lang.annotation.AnnotationFormatError;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 辛鑫 on 15/10/27.
 */
public class DownloadContainFragment extends Fragment {

    @Bind(R.id.download_tab)
    TabLayout downloadTab;
    @Bind(R.id.download_viewPager)
    ViewPager downloadViewPager;
    private MyAdapter adapter;
    private List<Fragment> fragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.downlaod_contain, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new MyAdapter(getFragmentManager());
        initData();
        downloadViewPager.setAdapter(adapter);
        downloadTab.setupWithViewPager(downloadViewPager);
        downloadTab.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorToolbar));

    }

    //初始化
    public void initData() {
        fragment = new ArrayList<Fragment>();
        AfterDownloadSongFragment frag = new AfterDownloadSongFragment();

        fragment.add(new AfterDownloadSongFragment());
        fragment.add(new IsDownloadingFragment());
        adapter.setData(fragment);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        for (Fragment frag : fragment) {
            getFragmentManager()
                    .beginTransaction()
                    .remove(frag)
                    .commit();
        }
    }

    //适配器
    private class MyAdapter extends FragmentPagerAdapter{
        private String[] title = {"单曲", "正在下载"};
        private List<Fragment> data;

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public void setData(List<Fragment> data) {
            this.data = data;
            notifyDataSetChanged();
        }


        @Override
        public Fragment getItem(int position) {
            return data != null && data.size() > 0 ? data.get(position) : null;
        }

        @Override
        public int getCount() {
            return data != null && data.size() > 0 ? data.size() : null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
